package com.training.hatton.dropboxclient.content.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.training.hatton.dropboxclient.content.contract.MetadataContract;

public class DBHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 3;

	private static final String DATABASE_NAME = "metadata.store.db";

	public static final String METADATA_TABLE = "TASKS";

	public static final String[] METADATA_COLUMNS = {
			MetadataContract.MetadataColumns.META_ID,
			MetadataContract.MetadataColumns.PATH,
			MetadataContract.MetadataColumns.IS_DIR,
			MetadataContract.MetadataColumns.BYTES,
			MetadataContract.MetadataColumns.NAME,
			MetadataContract.MetadataColumns.PARENT,
			MetadataContract.MetadataColumns.REV,
			MetadataContract.MetadataColumns.JSON,
			MetadataContract.MetadataColumns.THUMB_EXISTS};

	public static final int ID_COL_INDEX = 0;
	public static final int PATH_COL_INDEX = 1;
	public static final int IS_DIR_COL_INDEX = 2;
	public static final int BYTES_COL_INDEX = 8;
	public static final int NAME_COL_INDEX = 3;
	public static final int PARENT_COL_INDEX = 4;
	public static final int REV_COL_INDEX = 5;
	public static final int JSON_COL_INDEX = 6;	
	public static final int THUMB_EXISTS_COL_INDEX = 7;
	
	public static final String CREATE_METADATA_TABLE_SQL = "CREATE TABLE "
			+ METADATA_TABLE + " (" + METADATA_COLUMNS[ID_COL_INDEX]
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ METADATA_COLUMNS[PATH_COL_INDEX] + " VARCHAR NOT NULL UNIQUE, "
			+ METADATA_COLUMNS[IS_DIR_COL_INDEX] + " VARCHAR NOT NULL, "
			+ METADATA_COLUMNS[BYTES_COL_INDEX] + " VARCHAR, "
			+ METADATA_COLUMNS[NAME_COL_INDEX] + " VARCHAR NOT NULL, "
			+ METADATA_COLUMNS[PARENT_COL_INDEX] + " VARCHAR NOT NULL, "
			+ METADATA_COLUMNS[JSON_COL_INDEX] + " VARCHAR NOT NULL, "
			+ METADATA_COLUMNS[THUMB_EXISTS_COL_INDEX] + " VARCHAR NOT NULL, "
			+ METADATA_COLUMNS[REV_COL_INDEX] + " VARCHAR NOT NULL)";

	public static final String DROP_METADATA_TABLE_SQL = "DROP TABLE IF EXISTS " + METADATA_TABLE;

	public DBHelper(final Context context, final CursorFactory factory) {
		super(context, DATABASE_NAME, factory, DATABASE_VERSION);
	}

	@Override
	public final void onCreate(final SQLiteDatabase db) {
		try {
			db.beginTransaction();
			db.execSQL(CREATE_METADATA_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}

	}

	@Override
	public final void onUpgrade(final SQLiteDatabase db, final int oldVersion,
			final int newVersion) {
		try {
			db.beginTransaction();
			db.execSQL(DROP_METADATA_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		onCreate(db);
	}

	public Cursor getMetadata() {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(DBHelper.METADATA_TABLE, null, null, null, null, null, null);
		return cursor;
	}

	public long addMetadata(ContentValues values) {
		SQLiteDatabase db = getWritableDatabase();
		return db.insert(DBHelper.METADATA_TABLE, null, values);
	}

	public int bulkInsert(ContentValues[] values) {
		SQLiteDatabase db = getWritableDatabase();
		int ids = 0;
		try {
			db.beginTransaction();
			for (ContentValues value : values) {
				db.insert(DBHelper.METADATA_TABLE, null, value);
				ids++;
			}
			db.setTransactionSuccessful();
			return ids;
		} finally {
			db.endTransaction();
		}
	}

}
